<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>shell 练习-成员分组 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>shell 练习-成员分组</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>需求是，把所有的成员平均得分成若干个小组。这里，我会提供一个人员列表，比如成员有50人，需要分成7个小组，要求随机性，每次和每次分组的结构应该不一致。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#假设成员列表文件为members.txt</span></div><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">f=members.txt</div><div class="line">n=`wc -l <span class="variable">$f</span>|awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line"></div><div class="line">get_n()</div><div class="line">&#123;</div><div class="line">    l=`<span class="built_in">echo</span> <span class="variable">$1</span>|wc -c`</div><div class="line">    n1=<span class="variable">$RANDOM</span></div><div class="line">    n2=$[<span class="variable">$n1</span>+<span class="variable">$l</span>]</div><div class="line">    g_id=$[<span class="variable">$n1</span>%7]</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$g_id</span> -eq 0 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">	g_id=7</div><div class="line">    <span class="keyword">fi</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$g_id</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 7`</div><div class="line"><span class="keyword">do</span></div><div class="line">    [ -f n_<span class="variable">$i</span>.txt ] &amp;&amp; rm -f n_<span class="variable">$i</span>.txt</div><div class="line"><span class="keyword">done</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 <span class="variable">$n</span>`</div><div class="line"><span class="keyword">do</span></div><div class="line">    name=`sed -n <span class="string">"<span class="variable">$i</span>"</span>p <span class="variable">$f</span>`</div><div class="line">    g=`get_n <span class="variable">$name</span>`</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$name</span> &gt;&gt; n_<span class="variable">$g</span>.txt</div><div class="line"><span class="keyword">done</span></div><div class="line"></div><div class="line"><span class="function"><span class="title">nu</span></span>()&#123;</div><div class="line">    wc -l <span class="variable">$1</span>|awk <span class="string">'&#123;print $1&#125;'</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">max</span></span>()&#123;</div><div class="line">    ma=0</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 7`</div><div class="line">    <span class="keyword">do</span></div><div class="line">	n=`nu n_<span class="variable">$i</span>.txt`</div><div class="line">	<span class="keyword">if</span> [ <span class="variable">$n</span> -gt <span class="variable">$ma</span> ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">	    ma=<span class="variable">$n</span></div><div class="line">	<span class="keyword">fi</span></div><div class="line">    <span class="keyword">done</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$ma</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">min</span></span>()&#123;</div><div class="line">    mi=50</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 7`</div><div class="line">    <span class="keyword">do</span></div><div class="line">	n=`nu n_<span class="variable">$i</span>.txt`</div><div class="line">	<span class="keyword">if</span> [ <span class="variable">$n</span> -lt <span class="variable">$mi</span> ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">	    mi=<span class="variable">$n</span></div><div class="line">	<span class="keyword">fi</span></div><div class="line">    <span class="keyword">done</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$mi</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">ini_min=1</div><div class="line"></div><div class="line"><span class="keyword">while</span> [ <span class="variable">$ini_min</span> -le 7 ]</div><div class="line"><span class="keyword">do</span></div><div class="line">    m1=`max`</div><div class="line">    m2=`min`</div><div class="line">    ini_min=m2</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 7`</div><div class="line">    <span class="keyword">do</span></div><div class="line">	n=`nu n_<span class="variable">$i</span>.txt`</div><div class="line">	<span class="keyword">if</span> [ <span class="variable">$n</span> -eq <span class="variable">$m1</span> ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">	    f1=n_<span class="variable">$i</span>.txt</div><div class="line">	<span class="keyword">elif</span> [ <span class="variable">$n</span> -eq <span class="variable">$m2</span> ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">	    f2=n_<span class="variable">$i</span>.txt</div><div class="line">	<span class="keyword">fi</span></div><div class="line">    <span class="keyword">done</span></div><div class="line">    name=`tail -n1 <span class="variable">$f1</span>`</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$name</span> &gt;&gt; <span class="variable">$f2</span></div><div class="line">    sed -i <span class="string">"/<span class="variable">$name</span>/d"</span> <span class="variable">$f1</span></div><div class="line">    ini_min=$[<span class="variable">$ini_min</span>+1]</div><div class="line"><span class="keyword">done</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 7`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"<span class="variable">$i</span> 组成员有："</span></div><div class="line">    cat n_<span class="variable">$i</span>.txt</div><div class="line">    <span class="built_in">echo</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Shell 练习/65.shell 练习-成员分组/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
